package com.atguigu.guli.service.edu.controller.api;


import com.atguigu.guli.service.base.dto.CourseDto;
import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.edu.entity.Course;
import com.atguigu.guli.service.edu.entity.query.CourseSeacherTwo;
import com.atguigu.guli.service.edu.entity.vo.CourseDetailsVo;
import com.atguigu.guli.service.edu.service.CourseService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2021-07-26
 */
@Api(tags = "用户的课程模块")
@RestController

@RequestMapping("/api/edu/course")
public class ApiCourseController {
    @Autowired
    CourseService courseService;

    @ApiOperation("根据课程id展示详情vo")
    @GetMapping("getCourseDetails/{id}")
    public R getCourseDetails(@PathVariable String id){
        CourseDetailsVo vo = courseService.getCourseDetails(id);

        return R.ok().data("item",vo);
    }

    @ApiOperation("课程展示")
    @GetMapping("list")
    public R getCourses(CourseSeacherTwo courseSeacher){
           List<Course> courses = courseService.listCourses(courseSeacher);
           return R.ok().data("items",courses);
    }

    @ApiOperation("根据讲师id获取课程列表")
    @GetMapping("getByTeacherId/{teacherId}")
    public R getListByTeacherId(@PathVariable String teacherId){
        LambdaQueryWrapper<Course> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(Course::getId,Course::getCover,Course::getTitle);
        queryWrapper.eq(Course::getTeacherId,teacherId);
        List<Course> courses = courseService.list(queryWrapper);
        return R.ok().data("items",courses);
    }

    @ApiOperation("根据课程id查询课程信息")
    @GetMapping("getCourseDto/{courseId}")
    public R getCourseDtoById(@PathVariable String courseId){
        CourseDto courseDto = courseService.getCourseDtoById(courseId);
        return R.ok().data("courseDto",courseDto);
    }

    @ApiOperation("更新销量方法远程调用")
    @PutMapping("updateSales/{courseId}")
    public R updateSales(@PathVariable String courseId){
        courseService.updateBuyCountById(courseId);
        return R.ok();
    }

}

